package com.guan.le;

/**
 * 插入排序
 */
public class InsertionSort {
    public static void main(String[] args) {
        Comparable[] array = {12, 34, 11, 2, 3, 6, 3, 12};
        insertionSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    public static <T extends Comparable> void insertionSort(T[] a) {
        int j;
        for (int p = 1; p < a.length; p++) {
            T tmp = a[p];
            for (j = p; j > 0 && tmp.compareTo(a[j - 1]) < 0; j--) {
                a[j] = a[j - 1];
            }
            a[j] = tmp;

        }
    }
}
